@tool
class_name ContextBehavior_Flee
extends BaseContextBehavior

#region 属性
@export var force:float
@export var flee_radius:float = 30
var targets:Array = []
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init():
	super()
func calc_steering(behaviors_owner:Node,agent:SteeringBehaviorAgent,delta:float):
	super(behaviors_owner,agent,delta)
	var detect_component:DetectComponent = agent.entity.get_component(DetectComponent)
	if not detect_component:
		return
	var detector_target:Detector_Target = detect_component.get_detector(GameEnum.DetectorType.Target)
	if not detector_target:
		return
	targets = detector_target.get_targets(agent.get_pos(),flee_radius,Callable(),-1)
	var interest = Vector2.ZERO
	for target in targets:
		var to_target = get_target_pos(target) - agent.get_pos()
		interest += -to_target.normalized() * to_target.length() / flee_radius
	agent.add_linear_force_interest(interest.normalized() * sensitive * force)
#endregion
#region 公共方法
func get_target_pos(target:Node2D) -> Vector2:
	return target.global_position
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _init() -> void:
	if Engine.is_editor_hint():
		resource_local_to_scene = true
	behavior_type = GameEnum.ContextBehaviorType.Flee
func draw() -> void:
	super.draw()
	GameSteeringBehaviorDebug.draw_circle(agent.position,flee_radius,Color.GREEN,false,2)
	for target in targets:
		GameSteeringBehaviorDebug.draw_circle(get_target_pos(target) - agent.get_pos(),2,Color.RED,true)
#endregion
